package org.jeecg.modules.system.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jeecg.modules.system.entity.SysConfig;
import org.jeecg.modules.system.mapper.SysConfigMapper;
import org.jeecg.modules.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

	@Autowired
	private SysConfigMapper sysConfigMapper;

	@Override
	public String get3rdSso() {
		try {
			String sso3rd = sysConfigMapper.queryConfigByCode("sso3rd");
			if (null != sso3rd && !sso3rd.trim().isEmpty()) {
				log.info("sso3rd={}", sso3rd);
				return sso3rd.trim();
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return "";
	}

	@Override
	public Map<String, String> load3rdSsoInfo(String ssoPrefix, String username, String password) {

		Map<String, String> results = new HashMap<String, String>();
		
		Set<String> ssoKeys = new HashSet<String>();
		ssoKeys.add("Url");
		ssoKeys.add("Headers");
		ssoKeys.add("Variables");
		ssoKeys.add("Body");
		
		for(String ssoKey : ssoKeys) {

			String valueKey = ssoPrefix + ssoKey;
			String value = sysConfigMapper.queryConfigByCode(valueKey);
			log.info("{}={}", valueKey, value);
			if (null == value) {
				continue;
			}
			value = value.trim();
			results.put(valueKey, value);
			
		}

		return results;
	}

}
